package com.benny_driving_dri.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.benny_driving_dri.act.TaximeterAct;
import com.benny_driving_dri.act.TaximeterAct2;

public class TaximeterSer2 extends Service {
	public static Handler handler;
	public static final int ACTION_START = 1;
	public static final int ACTION_PAUSE = 2;
	private TimerTask timerTask;
	private Timer timer;
	private long timerCount;
	private static final String TAG = "TaximeterSer2";
	private boolean timerLooper;
	private SharedPreferences sp_taximeter;
	private Editor editor;

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initHandler();
		initData();
	}

	private void initHandler() {
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.arg1) {
				case ACTION_START:
					timerLooper = true;
					timerCount = sp_taximeter.getLong(
							TaximeterAct2.SP_KEY_WAITINGTIME, 0);
					initTimer();
					timer.schedule(timerTask, 1000, 1000);
					break;
				case ACTION_PAUSE:
					timerLooper = false;
					break;
				}
			}
		};
	}

	private void initData() {
		timerLooper = false;
		sp_taximeter = getSharedPreferences(TaximeterAct2.SP_NAME,
				Context.MODE_PRIVATE);
		editor = sp_taximeter.edit();
		timerCount = sp_taximeter.getLong(TaximeterAct2.SP_KEY_WAITINGTIME, 0);
	}

	private void initTimer() {
		timer = null;
		timerTask = null;
		timer = new Timer(true);
		timerTask = new TimerTask() {
			@Override
			public void run() {
				Log.i(TAG, "timerTask run");
				timerCount += 1;
				if (timerLooper) {
					// Log.i(TAG, "timerCount: " + timerCount);
					Log.i(TAG, "seconds: " + timerCount);
					if (null != TaximeterAct2.handler)
						TaximeterAct2.handler.sendMessage(TaximeterAct2.handler
								.obtainMessage(0,
										TaximeterAct2.HANDLER_KEY_UPDATE_TIMER,
										0, timerCount));
				} else {
					editor.putLong(TaximeterAct2.SP_KEY_WAITINGTIME, timerCount)
							.apply();
					timer.cancel();
					TaximeterAct2.handler
							.sendMessage(TaximeterAct2.handler
									.obtainMessage(
											0,
											TaximeterAct2.HANDLER_KEY_TIMER_PAUSE_LOOPBACK,
											0, timerCount));
				}
			}
		};
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (null != TaximeterAct2.handler) {
			Bundle b = intent.getExtras();
			TaximeterAct2.handler.sendMessage(TaximeterAct2.handler
					.obtainMessage(0,
							TaximeterAct2.HANDLER_KEY_SERVICE_START_FINISHED,
							b.getInt(TaximeterAct2.SERVICE_START_ACTION_KEY)));
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		editor.putLong(TaximeterAct2.SP_KEY_WAITINGTIME, timerCount).apply();
		timerLooper = false;
		timer.cancel();
	}

}
